482A - Diverse Permutation - CodeForces Solution


constructive algorithms greedy *1200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <bitset>
#include <algorithm>
using namespace std;

#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vll vector<long long>
#define mii map<int, int>
#define si set<int>
#define sc set<char>
#define pq priority_queue<long long>
#define lb lower_bound
#define ub upper_bound
#define f(i,s,e) for(long long int i=s;i<e;i++)
#define cf(i,s,e) for(long long int i=s;i<=e;i++)
#define rf(i,e,s) for(long long int i=e-1;i>=s;i--)
#define pb push_back
#define eb emplace_back
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
#define read(type) readInt<type>()
#define np next_permutation

int main(){
    ll n,k;
    cin>>n>>k;
    for(int i=1;i<=n-k;i++)
    {
        cout<<i<<" ";
    }
    int p=0;
    int a=n;
    int b=n-k+1;
    while(k--)
    {
        if(p%2==0)
        {
            cout<<a<<" ";
            p++;
            a--;
        }
        else
        {
            cout<<b<<" ";
            p++;
            b++;
        }
    }
}


Comments

Submit
0 Comments
More Questions

1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element